extends Control


const target_file = "res://destiny_mod.dym"


# 需要导出的文件




# 请导出目录以 / 结尾
# mod资源
const export_area_file = [
#		"res://assets/",
		"res://map/assets/",
		"res://map/data/",
		"res://areaData.json",
	]


# 系列资源
const export_series_file = [
		"res://assets/",
		"res://seriesData.json",
	]



const map_path = "res://map/main/game/"

const map_json_path = "res://map/data/map.json"

# 将项目导出为json数据
func _on_export_button_up():
	package()
	


# 导出 区域
func package():
	var top_json = {
		"resource":{
			# key：{}
		},
		"map":{
			
		}
	}
	
	package_map(map_path, top_json)
	
	FileHelper.save(map_json_path, top_json)
	
	var target_file = self.target_file+"_area.zip"
	
	# 添加基础数据
	var mod_data = ExportModDataUtil.export_mod_data()
	
	FileHelper.save("res://areaData.json", mod_data)

	var out_file = File.new()
	out_file.open(target_file+"t", File.WRITE_READ)
	
	
	for file_name in export_area_file:
		if file_name.ends_with("/"):
			write_dir(file_name, out_file)
			package_dir(file_name, out_file)
		else:
			package_file(file_name, out_file)
	
	out_file.flush()

	out_file.seek(0)
	
	var out_len = out_file.get_len()
	var buffer = out_file.get_buffer(out_len)
	
	# 压缩
	var compress = buffer.compress(File.COMPRESSION_GZIP)
	
	
	var compress_file = File.new()
#	if ModeConfig.config["release"]:
#		compress_file.open_encrypted_with_pass(target_file, File.WRITE, ModeConfig.config["secret"])
#	else:
		# 加密
	compress = AESUtil.encrypt(compress,ModeConfig.config["secret"])
	compress_file.open(target_file, File.WRITE)

	# 写入原始数据大小
	
	compress_file.store_32(out_len)
	compress_file.store_buffer(compress)
	compress_file.close()
	out_file.close()
	
	print("打包区域完成==》", target_file)
	print("未压缩大小=>", out_len)
	print("压缩后大小=>", compress.size())
	
	
	
	
	Directory.new().remove(target_file+"t")



# 打包地图
func package_map(pre_dir, top_json):
	# 打开目录并读取所有文件  
	var dir = Directory.new()  
	if dir.open(pre_dir) == 0:
		dir.list_dir_begin(true)
		var file_name = dir.get_next()
		
		while file_name != "":
			if dir.current_is_dir():
				package_map(str(pre_dir,file_name,"/"), top_json)
			else:
				if file_name.ends_with(".tscn"):
					# 解析地图
					ModMapParseUtil.parse_tscn(str(pre_dir,file_name) ,top_json)
	
			file_name = dir.get_next()
	else:
		print("尝试访问路径时出错。")
		
# 打包
func package_dir(pre_dir:String, out_file:File):
		
	# 打开目录并读取所有文件  
	var dir = Directory.new()  
	
	var file_name = pre_dir
	
	if dir.open(pre_dir) == 0:
		dir.list_dir_begin(true)
		
		file_name = dir.get_next()
		
		while file_name != "":
			if dir.current_is_dir():
				
				var file_path = str(pre_dir, file_name,"/")
				write_dir(file_path, out_file)
				package_dir(file_path, out_file)
				
			else:
				package_file(str(pre_dir,file_name), out_file)
					

			file_name = dir.get_next()
	else:
		print("尝试访问路径时出错。")


func write_dir(file_path, out_file):
	# 目录也需要被写入
	# 名称去除res:// 前缀
	var name = file_path.replace("res://", "")
	var name_byte = name.to_utf8()
	
	out_file.store_32(name_byte.size())
	out_file.store_32(0)
	# 目录不加密
	out_file.store_8(0)
	
	out_file.store_buffer(name_byte)


func package_file(file_name, out_file):
	if !file_name.ends_with(".import"):		
		var read_file = File.new()
		
		var rst = read_file.open(file_name, File.READ)
		
		if rst == OK:
			var length = read_file.get_len()
			# 是否加密
			var is_encry = false
			for item in ModeConfig.config["encry_file"]:
				is_encry = file_name.begins_with(item)
				if is_encry: break
			
			# 名称去除res:// 前缀
			var name = file_name.replace("res://", "")
			var name_byte = name.to_utf8()
			
			var content_byte = read_file.get_buffer(length)
			
			# 写入名称与内容的长度
			out_file.store_32(name_byte.size())
			out_file.store_32(content_byte.size())
			
			print(name, " ", name_byte.size(), " ", content_byte.size())
			
			# 是否加密
			if is_encry:
				out_file.store_8(1)
			else:
				out_file.store_8(0)
				
			out_file.store_buffer(name_byte)
			# 写入内容
			out_file.store_buffer(content_byte)
		



